/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.maps;

import java.io.File;
import rpg.util.BinaryMapHandler;
import rpg.util.LineReader;

/**
 * rothens.tumblr.com
 *
 * @author Rothens
 */
public class MapHandler {

    public static Tiles tileset = new Tiles();
    BinaryMapHandler bmap = new BinaryMapHandler();


    public static final int LEFT = 0; //1
    public static final int UP = 1; //2
    public static final int RIGHT = 2; //4
    public static final int DOWN = 3; //8
    public static final int UPLEFT = 4;  //16
    public static final int UPRIGHT = 5; //32
    public static final int DOWNRIGHT = 6;  //64
    public static final int DOWNLEFT = 7;  //128


    public MapTile getTile(int actual, int dir, Map map) {
        switch (dir) {
            case UP:
                if (actual - map.width < 0) {
                    return map.tiles[actual];
                }
                return map.tiles[actual - map.width];
            case LEFT:
                if (actual % map.width == 0) {
                    return map.tiles[actual];
                }
                return map.tiles[actual - 1];
            case DOWN:
                if (actual + map.width >= map.width * map.height) {
                    return map.tiles[actual];
                }
                return map.tiles[actual + map.width];
            case RIGHT:
                if (actual % map.width == map.width - 1) {
                    return map.tiles[actual];
                }
                return map.tiles[actual + 1];
            case UPLEFT:
               if (actual - map.width < 0 || actual % map.width == 0) {
                    return map.tiles[actual];
                }
                return map.tiles[actual - map.width - 1];
            case UPRIGHT:
                if (actual - map.width < 0 || actual % map.width == map.width - 1) {
                    return map.tiles[actual];
                }
                return map.tiles[actual - map.width + 1];
            case DOWNRIGHT:
                if (actual + map.width >= map.width * map.height || actual % map.width == map.width - 1) {
                    return map.tiles[actual];
                }
                return map.tiles[actual + map.width + 1];
            case DOWNLEFT:
                if (actual + map.width >= map.width * map.height || actual % map.width == 0) {
                    return map.tiles[actual];
                }
                return map.tiles[actual + map.width - 1];
        }
        return null;
    }

    public MapHandler() {
        tileset.loadTiles();
    }

    public Map loadMap(File file1) {
        //String name = file1.getPath();
        String mapname = file1.getName().replaceFirst("[.][^.]+$", "");
        LineReader lr = new LineReader(file1);
        int w = Integer.parseInt(lr.next());
        if (!lr.hasNext()) {
            return null;
        }
        int h = Integer.parseInt(lr.next());
        String data = "";
        while (lr.hasNext()) {
            String next = lr.next();
            if (next.equals("--")) {
                break;
            }
            data += next;
        }
        String layer1 = "";
        while (lr.hasNext()) {
            String next = lr.next();
            if (next.equals("--")) {
                break;
            }
            layer1 += next;
        }
        String layer2 = "";
        while (lr.hasNext()) {
            String next = lr.next();
            if (next.equals("--")) {
                break;
            }
            layer2 += next;
        }
        String road = "";
        while (lr.hasNext()) {
            String next = lr.next();
            if (next.equals("--")) {
                break;
            }
            road += next;
        }
        String entityData = "";
        while (lr.hasNext()) {
            String next = lr.next();
            if (next.equals("--")) {
                break;
            }
            entityData += next;
        }

        return new Map(mapname, w, h, data, layer1, layer2, road, entityData, this);
    }

    public Map loadBinaryMap(String name) {
        return bmap.loadBinaryMap(name, this);
    }
}
